package keye.document_manage.gui;

import java.awt.Color;
import java.awt.Component;

import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;

public class MyTableCellRenderer extends DefaultTableCellRenderer{
	private static final long serialVersionUID = 1L;

	private Color selectionColor = new Color(207,228,249);//行选择颜色
	private Color evenRowColor = new Color(233,242,241);//奇数行颜色
	private Color oddRowColor = new Color(255,255,255);//偶数行颜色
	
	@Override
	public Component getTableCellRendererComponent(JTable table, Object value,
			boolean isSelected, boolean hasFocus, int row, int column) {

		Component cell =  super.getTableCellRendererComponent(table, value, isSelected, hasFocus,
				row, column);
//		System.out.println("getTableCellRendererComponent column:"+column+"  row:"+row);
		this.setColor(cell, table, isSelected, hasFocus, row, column);
		return cell;
	}

	@Override
	public void setHorizontalAlignment(int alignment) {
		// TODO Auto-generated method stub
		
		super.setHorizontalAlignment(JLabel.CENTER);
	}
	
	private void setColor(Component component, JTable table, boolean isSelected, boolean hasFocus, int row, int column){
		if(isSelected){
			component.setBackground(this.selectionColor);
			setBorder(null);//去掉边
		}else{
			if(row%2 == 0){
				component.setBackground(evenRowColor);
			}else{
				component.setBackground(oddRowColor);
			}
		}
	}
	
}
